<?php

/**
 * IndexController - The default controller class
 * 
 * @author
 * @version 
 */

require_once 'Zend/Controller/Action.php';

class Adminb_AuctionController extends Zend_Controller_Action 
{
	

	
	public function init(){
		
		$member=new CMember();
		
		if(!$member->isUser($this->_request->getModuleName(),$this->_request->getControllerName())){
			
			if($_COOKIE[COOKIE_PRE.'uid']!=''){
				
				$this->_redirect(MY_URL.'/buyer');
				
			}else{
			
				$this->_redirect(MY_URL.'/buyer-auth-login?preurl=');
				
			}
			
		}
		
	}
	
	public function preDispatch()
	{

		$this->getHelper ('layout')->setLayout('framelayout');
		
		$this->view->addHelperPath(MY_ROOT.'/vhelpers');
		
		Zend_Controller_Action_HelperBroker::addPath(MY_ROOT.'/chelpers');

	}

	
    public function myauctionAction(){
    	
    	
    	
    }
    
    public function winauctionAction(){
    	
    	$page=$this->_request->getParam('page',1);
    	
    	$type=$this->_request->getParam('type','all');
    	
    	$item=new CItem();
    	
    	$item->setTypeTable(R_PRE.'productfls');
    	
    	if($type=='all'){
    		
    		$where="win_username='".$_COOKIE[COOKIE_PRE.'username']."' AND (add_time+valid_seconds)<=".time();
    		
    	}else if($type=='paid'){
    		
    		$where="win_username='".$_COOKIE[COOKIE_PRE.'username']."' AND has_paid='1'";
    		
    	}else if($type=='notpaid'){
    		
    		$where="win_username='".$_COOKIE[COOKIE_PRE.'username']."' AND has_paid='0'AND (add_time+valid_seconds)<=".time();
    		
    		$itemArray=$item->getAllItems(0,0,'auction_product',0,0,'has_banned',$where);
    		
    		if(count($itemArray)>0){
    		
    			$pageArray['pageNumber']=1;
    			
    		}else{
    			
    			$pageArray['pageNumber']=0;
    			
    		}
    		
    		$this->view->assign ( "pageArray", $pageArray );
    		
    		$this->view->assign('itemArray',$itemArray);
    		
    		$this->view->assign('type',$type);
    		
    		return true;
    		
    	}
    	
    	$itemCounts=$item->getAllItemCounts('auction_product',0,0,'has_banned',$where);
    	
    	Zend_Loader::loadClass("CFilter");
    	
    	$pageArray = CFilter::filterPage ( $page, $itemCounts, 10 );
    	
    	$itemArray=$item->getAllItems($pageArray['from'],10,'auction_product',0,0,'has_banned',$where);
    	
    	Zend_Loader::loadClass("CPage");
    	
    	$pageString = CPage::divPages( $pageArray ['pageNumber'], $pageArray ['page'], ROOT_PATH."/adminb-auction-winauction?type=$type"); //生成分页字符串
    	
    	$this->view->assign ( "pageString", $pageString );
    	
    	$this->view->assign ( "pageArray", $pageArray );
		
		$this->view->assign('itemArray',$itemArray);
		
		$this->view->assign('type',$type);
		
		return true;
    	
    }

    public function moreAction(){
    	
    	$itemid=$this->_request->getParam('itemid');
    	
    	$item=new CItem();
    	
    	$item->setTypeTable(R_PRE.'productfls');
    	
    	$itemArray=$item->getDetail($itemid,'auction_product');
    	
    	$this->view->assign('itemArray',$itemArray);
    	
    	$attachment=new CAttachment();
    	
    	$attachmentArray=$attachment->getAttachments($itemid);//附件数组
    	
    	$this->view->assign('attachmentArray',$attachmentArray);
    	
    }
    
    public function contactAction(){
    	
    	$address=new CAddress();
		
		$addressArray=$address->getAllAddresses($_COOKIE[COOKIE_PRE.'uid']);

		$this->view->assign('addressArray',$addressArray);
		
		
		$location=new CFenlei('location');
				
 		$fenleiArray=$location->getFenleiByPid(0);
 	
 		$this->view->assign('fenleiArray',$fenleiArray);
		
		
		$contact=new COrderContact();
				
		$contactArray=$contact->getAllContacts(0,1,$_COOKIE[COOKIE_PRE.'uid']);
		
		$this->view->assign('contactArray',$contactArray);
    	
    }
    
    public function docontactAction(){
    	
    	$this->_helper->viewRenderer->setNoRender();
		
		$this->getHelper ('layout')->disableLayout ();
    	
   		$paramArray=$this->_request->getParams();
	
		$filters = array(
    		'*'   => 'StringTrim'
		);

		$validators = array(
		);
		
		$input = new Zend_Filter_Input($filters, $validators, $paramArray);
		
		$addressid=$input->addressid;
		
		$address=new CAddress();
		
		if($addressid==0){
			
			$address=new CAddress();
			
			$addressid=$address->createAddress(array('uid'=>$_COOKIE[COOKIE_PRE.'uid'],'content'=>$input->address,'add_time'=>time()));
			
		}
		
		$addressArray=$address->getDetail($addressid);
	
		$contact=new COrderContact();
	
		$data=array(
					'uid'=>$_COOKIE[COOKIE_PRE.'uid'],
					'email'=>$_COOKIE[COOKIE_PRE.'username'],
					'company_name'=>$input->company_name,
					'contact_name'=>$input->contact_name,
					'address'=>$addressArray[0]['content'],
					'location_id'=>$input->location_id,
					'code'=>$input->code,
					'city'=>$input->city,
					'phone_number'=>$input->phone_number,
					'fax_number'=>$input->fax_number
				);
				
		if($data['city']==''||$data['location_id']=='0'){
			
			$this->_redirect(MY_URL.'/adminb-auction-contact');
			
		}else{
			
			$contactArray=$contact->getAllContacts(0,1,$_COOKIE[COOKIE_PRE.'uid']);
			
			if($contactArray[0]['contactid']!=''){
				
				$contact->editContact($contactArray[0]['contactid'],$data);
				
			}else{
				
				$contact->createContact($data);
				
			}
			
			$this->_redirect(MY_URL.'/adminb-auction-shipping?location_id='.$input->location_id);
			
		}
    	
    }
    
    public function shippingAction(){

    	$location_id=$this->_request->getParam('location_id');
    	
    	$item=new CItem();
    	
    	$item->setTypeTable(R_PRE.'productfls');
    	
    	$where="win_username='".$_COOKIE[COOKIE_PRE.'username']."' AND has_paid='0' AND (add_time+valid_seconds)<=".time();
    		
    	$itemArray=$item->getAllItems(0,0,'auction_product',0,0,'has_banned',$where);
    	
    	$total=0;
    	
    	$weight=0;
    	
    	$volume=0;
    	
    	foreach ($itemArray as $value){
    		
	    	$total+=$value['fixed_price'];
	    	
	    	if($value['is_free_shipment']=='0'){
	    		
	    		$weight+=$value['weight'];
	    	    	
	    		$size=explode('*',$value['size']);
	    		
	    		$volume+=$size[0]*$size[1]*$size[2];
    		
    		}
    		
    	}
    	
    	if($weight==0){
    		
    		$this->_redirect(MY_URL.'/adminb-auction-pay');
    		
    	}else{
    	
	    	$express=$this->getHelper('Express');
	    	
	    	$expressArray=$express->getExpressArray($volume,$weight,$location_id);
	    	
	    	$this->view->assign('expressArray',$expressArray);
	    	
	    	$this->view->assign('weight',$express->getWeight());
    	
    	}
    	
    }
    
    public function payAction(){
    	/*
    	if(!$this->getRequest()->isPost()){
    		
    		$this->_helper->viewRenderer->setNoRender();
			
			$this->view->showMsg("发生未知错误!");
			
			return false;
			
		}*/
    	
    	$carrier=$this->_request->getParam('carrier');

    	$item=new CItem();
    	
    	$item->setTypeTable(R_PRE.'productfls');
    	
    	$where="win_username='".$_COOKIE[COOKIE_PRE.'username']."' AND has_paid='0' AND (add_time+valid_seconds)<=".time();
    		
    	$itemArray=$item->getAllItems(0,0,'auction_product',0,0,'has_banned',$where);

    	
    	$this->view->assign('itemArray',$itemArray);
    	
    	
    	$contact=new COrderContact();
				
		$contactArray=$contact->getAllContacts(0,1,$_COOKIE[COOKIE_PRE.'uid']);
		
		$this->view->assign('contactArray',$contactArray);
		
		
		$location_id=$contactArray[0]['location_id'];
    	
    	$itemTotal=0;
    	
    	$weight=0;
    	
    	$volume=0;
    	
    	foreach ($itemArray as $value){
    		
	    	$itemTotal+=$value['fixed_price'];
	    	
	    	if($value['is_free_shipment']=='0'){
	    		
	    		$weight+=$value['weight'];
	    	    	
	    		$size=explode('*',$value['size']);
	    		
	    		$volume+=$size[0]*$size[1]*$size[2];
    		
    		}
    		
    	}
    	
    	$itemTotal=round($itemTotal,2);
    	
    	$shippingCost=round($this->getHelper('Express')->calculate($volume,$weight,$location_id,$carrier),2);
    	
    	$total=round($itemTotal+$shippingCost,2);
    	
    	
    	$this->view->assign('itemTotal',$itemTotal);
    	
    	$this->view->assign('carrier',$carrier);
    	
    	$this->view->assign('shippingCost',$shippingCost);
    	
    	$this->view->assign('total',$total);
    	
    	$member=new CMember();
    	
    	$memberArray=$member->getUserDetails(array('uid'=>$_COOKIE[COOKIE_PRE.'uid']));
    	
    	$this->view->assign('virtualMoney',$memberArray[0]['virtual']);
    	
    	$virtual=new CVirtual();
    	
    	$virtual_exchange_rate=$virtual->getDetail('virtual_exchange_rate');
    	
    	$this->view->assign('virtual_exchange_rate',$virtual_exchange_rate);
    	
    	$this->view->assign('shouldSubVirtual',(int)($virtual_exchange_rate*$total));
    	
    	return true;
    	
    }
    
    public function dopayAction(){
    	
    	$this->_helper->viewRenderer->setNoRender();
    	
    	$this->payAction();
    	
    	$itemArray=$this->view->itemArray;
    	
    	$itemidArray=array();
    	
    	foreach ($itemArray as $value){
    		
    		$itemidArray[]=$value['itemid'];
    		
    	}

    	$order=new CAuctionOrder();
    	
    	$data=array(
    				'uid'=>$_COOKIE[COOKIE_PRE.'uid'],
    				'flid'=>1,
    				'order_string'=>serialize($this->view->itemArray),
    				'money'=>$this->view->total,
    				'cost_virtual'=>$this->view->shouldSubVirtual,
    				'shipping_cost'=>$this->view->shippingCost,
    				'add_time'=>time()
    				);
    	
    	$order->createOrder($data);
    	
    	$product=new CAuctionProduct();
    	
    	$product->editProducts($itemidArray,array('has_paid'=>1));
    	
    	$virtual=new CVirtual();
    	
    	$virtual->subVirtual($this->view->shouldSubVirtual,"WHERE uid='".$_COOKIE[COOKIE_PRE.'uid']."'");
    	
    	$this->view->showMsg("交易成功！","adminb-auction-winauction?type=paid");
    	
    }
    
    public function cancelAction(){
    	
    	$this->_helper->viewRenderer->setNoRender();
    	
    	$itemid=$this->_request->getParam('itemid');
    	
    	$product=new CAuctionProduct();
    	
    	$product->editProduct($itemid,array('win_username'=>''));
    	
    	$this->view->showMsg("You hava canceled this order successfully!");
    	
    }
	
	public function postAction(){
	
		//$this->_helper->viewRenderer->setNoRender();
		
		//$this->getHelper ('layout')->disableLayout ();
	
	}
    

    
}
